/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.listener;

import com.minierp.helper.EncCommonHelper;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;

public class FileUploadServer extends Thread {
	private ServletContext context;
	private Socket client;

	public FileUploadServer(Socket c, ServletContext ct) {
		this.client = c;
		this.context = ct;

		try {
			c.setSoTimeout(20000);
		} catch (Exception arg3) {
			;
		}

	}

	public void run() {
		BufferedInputStream in = null;
		PrintWriter out = null;

		try {
			in = new BufferedInputStream(this.client.getInputStream());
			out = new PrintWriter(this.client.getOutputStream());
			String ex = "";
			String uploadDate = "";
			String signId = "";
			String uploadVirtualPath = "";
			String uploadRealPath = "";
			String targetFileName = "";
			String tmpFileName = "";
			String allowExist = "";
			FileOutputStream fo = null;
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

			while (true) {
				String returnStr;
				label541 : {
					byte[] b = new byte[102500];
					int num = in.read(b);
					if (num == -1) {
						EncCommonHelper
								.debug("**********file upload:client socket closed...");
					} else {
						int headerEndIndex = 0;
						int headerStr = 0;

						while (true) {
							if (headerStr < 100) {
								if (b[headerStr] != 0) {
									++headerStr;
									continue;
								}

								headerEndIndex = headerStr;
							}

							String arg57 = new String(b, 0, headerEndIndex);
							Map map = this.parseHeader(arg57);
							String command = (String) map.get("command");
							if (command != null) {
								returnStr = "";
								if (command.equals("file_name")) {
									signId = map.get("sign_id").toString();
									uploadDate = map.get("upload_date")
											.toString();
									allowExist = map.get("allow_exist")
											.toString();
									ex = new String(b, 100, num - 100);
									int arg59 = ex.lastIndexOf("\\");
									if (arg59 != -1) {
										ex = ex.substring(arg59 + 1);
									}

									if (ex.equals("") || signId.equals("")
											|| uploadDate.equals("")) {
										throw new Exception(
												"Parameter error on file_name command!");
									}

									uploadVirtualPath = EncCommonHelper
											.getUploadVirtualPath(this.context,
													uploadDate);
									uploadRealPath = this.context
											.getRealPath(uploadVirtualPath);
									targetFileName = uploadRealPath
											+ System.getProperty("file.separator")
											+ ex;
									tmpFileName = targetFileName + "." + signId;
									File arg60 = new File(tmpFileName);
									Date arg62 = new Date();
									if (!arg60.exists()
											&& !sf.format(sf.parse(uploadDate))
													.equals(sf.format(arg62))) {
										returnStr = "CLIENT_TIME_ERROR";
									} else {
										File arg63 = new File(
												uploadRealPath
														+ System.getProperty("file.separator")
														+ ex);
										if (!allowExist.equals("1")
												&& arg63.exists()) {
											returnStr = "FILE_EXIST";
										} else {
											returnStr = "OK";
										}
									}
									break label541;
								}

								if (!ex.equals("")) {
									if (command.equals("data")) {
										long file = Long.parseLong(map.get(
												"start_index").toString());
										long targetFile = 0L;
										File lastIndex = new File(tmpFileName);
										if (lastIndex.exists()) {
											targetFile = lastIndex.length();
										}

										if (targetFile == file) {
											try {
												fo = new FileOutputStream(
														lastIndex, true);
												fo.write(b, 100, num - 100);
												fo.flush();
												fo.close();
											} finally {
												try {
													if (fo != null) {
														fo.close();
													}
												} catch (Exception arg53) {
													;
												}

											}
										}

										returnStr = String.valueOf(lastIndex
												.length());
									} else {
										File arg58;
										if (command.equals("end")) {
											arg58 = new File(tmpFileName);
											if (arg58.exists()) {
												String virFile = uploadVirtualPath
														+ ex;
												File arg61 = new File(
														targetFileName);
												if (allowExist.equals("1")) {
													int i = 0;
													int arg64 = ex
															.lastIndexOf(".");
													String partName = ex;
													String extendName = "";
													if (arg64 != -1) {
														extendName = ex
																.substring(arg64 + 1);
														partName = ex
																.substring(0,
																		arg64);
													}

													while (arg61.exists()) {
														++i;
														targetFileName = uploadRealPath
																+ System.getProperty("file.separator")
																+ partName
																+ "("
																+ i
																+ ")."
																+ extendName;
														arg61 = new File(
																targetFileName);
														virFile = uploadVirtualPath
																+ partName
																+ "("
																+ i
																+ ")."
																+ extendName;
													}
												}

												if (!arg61.exists()) {
													arg58.renameTo(arg61);
													returnStr = virFile;
												} else {
													returnStr = "FILE_EXIST";
												}
											} else {
												returnStr = "TMP_FILE_NOT_EXIST";
											}
										} else if (command.equals("cancel")) {
											arg58 = new File(tmpFileName);
											if (arg58.exists()) {
												arg58.delete();
											}

											returnStr = "CLEAR";
										}
									}
									break label541;
								}
							}
							break;
						}
					}

					EncCommonHelper.debug("************file upload:accept end");
					break;
				}

				out.write(returnStr);
				out.flush();
			}
		} catch (Exception arg55) {
			arg55.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception arg52) {
				;
			}

			try {
				if (out != null) {
					out.close();
				}
			} catch (Exception arg51) {
				;
			}

			try {
				this.client.close();
			} catch (Exception arg50) {
				;
			}

		}

	}

	private Map<String, String> parseHeader(String headerStr) {
		HashMap map = new HashMap();
		String[] paras = headerStr.split(";");

		for (int i = 0; i < paras.length; ++i) {
			String[] nv = paras[i].split("=");
			if (nv.length == 2) {
				map.put(nv[0], nv[1]);
			}
		}

		return map;
	}

	public static void main(String[] args) throws IOException {
	}
}